<?php
// This file is part of CorfoWS for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * @package   local_corfows
 * @author    Hackware Human <human@hackware.cl>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace local_corfows;

defined('MOODLE_INTERNAL') || die();

use \mod_customcert\certificate;

class observers {
    public static function customcert($event) {
        // Only when downloading own certificate.
        // Other sanity checks are performed previous to event trigger.
        $downloadown = optional_param('downloadown', false, PARAM_BOOL);
        if (!$downloadown) {
            return;
        }

        global $DB, $USER;
        $customcertid = $event->get_data()['objectid'];
        $issue = static::get_issue($customcertid, $USER->id);

        if (!$issue) {
            certificate::issue_certificate($customcertid, $USER->id);
            $issue = static::get_issue($customcertid, $USER->id);
            $validation = false;
        } else {
            $validation = $DB->record_exists(
                'local_corfows_validation',
                array('issue_id' => $issue->id)
            );
        }

        // If validation exists there's nothing else to do.
        if (!$validation) {
            $response = http_request::validation($issue->code);
            if ($response !== false) {
                if ((
                    property_exists($response, 'Success') && $response->Success
                ) || (
                    property_exists($response, 'Message')
                    && strpos($response->Message, 'ya tiene cargada') !== false
                )) {
                    // Save validation.
                    $DB->insert_record(
                        'local_corfows_validation',
                        (object) array('issue_id' => $issue->id)
                    );
                    return;
                }
            }

            if (property_exists($response, 'Message')) {
                $errormsg = $response->Message;
            } elseif (property_exists($response, 'error_description')) {
                $errormsg = $response->error_description;
            } else {
                $errormsg = json_encode($response);
            }
            \debugging("CORFO Error: $errormsg", DEBUG_DEVELOPER);
        }
    }

    protected static function get_issue($customcertid, $userid) {
        global $DB;

        return $DB->get_record(
            'customcert_issues',
            array('userid' => $userid, 'customcertid' => $customcertid)
        );
    }
}
